﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JustPingPong
{
    class Program
    {
        static int ballPositionX = 0;
        static int ballPositionY = 0;
        static int firstPlayerPosition = 0;
        static int secondPlayerPosition = 0;
        static int firstPlayerScore = 0;
        static int secondPlayerScore = 0;
        static Random randomGenerator = new Random();
        static bool ballDirectionUp = true;
        static bool ballDirectionRight = true;
        static int difficulty = 0;
        static int firstPlayerPaddleSize = 6;
        static int secondPlayerPaddleSize = 6;


        static void SelectDifficulty()
        {
            Console.Write("Select Difficulty from 1 to 5! - ");
            difficulty = int.Parse(Console.ReadLine());
            while (difficulty > 5 || difficulty < 1)
	        {
	            Console.Write("Wrong difficulty input!");
                difficulty = int.Parse(Console.ReadLine());
	        }
        }

        static void SetBallAtTheMiddle()
        {
            ballPositionX = Console.WindowWidth / 2;
            ballPositionY = Console.WindowHeight / 2;
        }

        static void SetInitialPositions()
        {
            firstPlayerPosition = (Console.WindowHeight - firstPlayerPaddleSize) / 2;
            secondPlayerPosition = (Console.WindowHeight - secondPlayerPaddleSize) / 2;
            SetBallAtTheMiddle();          
        }

        static void DrawBall()
        {
            PrintAtPosition(ballPositionX, ballPositionY, '@');
        }

        static void DrawFirstPlayer()
        {
            for (int i = firstPlayerPosition; i < firstPlayerPosition + firstPlayerPaddleSize; i++)
            {
                PrintAtPosition(0, i, '|');
                PrintAtPosition(1, i, '|');
            }
        }

        static void DrawSecondPlayer()
        {
            for (int i = secondPlayerPosition; i < secondPlayerPosition + secondPlayerPaddleSize; i++)
            {
                PrintAtPosition(Console.WindowWidth - 1, i, '|');
                PrintAtPosition(Console.WindowWidth - 2, i, '|');
            }
        }

        static void PrintAtPosition(int x,int y,char symbol)
        {
            Console.SetCursorPosition(x, y);
            Console.Write(symbol);
        }

        static void RemoveScrollBars()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Red;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth; 
        }


        static void PrintResult()
        {
            Console.SetCursorPosition(Console.WindowWidth / 2 - 1, 0);
            Console.Write("{0}-{1}", firstPlayerScore, secondPlayerScore);
        }

        static void MoveFirstPlayerUp()
        {
            if (firstPlayerPosition > 0)
            {
                firstPlayerPosition--;
            }
        }

        static void MoveFirstPlayerDown()
        {
            if (firstPlayerPosition < Console.WindowHeight - firstPlayerPaddleSize)
            {
                firstPlayerPosition++;
            }
        }

        static void MoveSecondPlayerUp()
        {
            if (secondPlayerPosition > 0)
            {
                secondPlayerPosition--;
            }
        }

        static void MoveSecondPlayerDown()
        {
            if (secondPlayerPosition < Console.WindowHeight - secondPlayerPaddleSize)
            {
                secondPlayerPosition++;
            }
        }

        static void SecondPlayerAIMove()
        {
            int randomNumber = randomGenerator.Next(1, 101);
            if (randomNumber <= 20*difficulty)
            {
                if (ballDirectionUp && ballDirectionRight)
                {
                    MoveSecondPlayerUp();
                }
                else if (ballDirectionUp == false && ballDirectionRight)
                {
                    MoveSecondPlayerDown();
                    
                }
            }
        }

        static void MoveBall()
        {
            if (ballPositionY == 0)
            {
                ballDirectionUp = false;
            }
            if (ballPositionY == Console.WindowHeight - 1)
            {
                ballDirectionUp = true;
            }
            if (ballPositionX == 0)
            {
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2);
                Console.WriteLine("Second player WINS a point!");
                SetBallAtTheMiddle();
                ballDirectionRight = true;
                ballDirectionUp = true;
                secondPlayerScore++;
                Console.ReadKey();
            }
            if (ballPositionX == Console.WindowWidth - 1)
            {
                Console.SetCursorPosition(Console.WindowWidth / 2 - 10, Console.WindowHeight / 2);
                Console.WriteLine("First player WINS a point!");
                SetBallAtTheMiddle();
                ballDirectionRight = false;
                ballDirectionUp = true;
                firstPlayerScore++;
                Console.ReadKey();
            }

            if (ballPositionX < 3)
            {
                if (ballPositionY >= firstPlayerPosition 
                    && ballPositionY < firstPlayerPosition + firstPlayerPaddleSize)
                {
                    ballDirectionRight = true;
                }
            }
            if (ballPositionX > Console.WindowWidth - 3)
            {
                if (ballPositionY >= secondPlayerPosition
                     && ballPositionY < secondPlayerPosition + secondPlayerPaddleSize)
                {
                    ballDirectionRight = false;
                }
            }

            if (ballDirectionUp)
            {
                ballPositionY--;
            }
            else
            {
                ballPositionY++;
            }

            if (ballDirectionRight)
            {
                ballPositionX++;
            }
            else
            {
                ballPositionX--;
            }
        }
        static void Main()
        {
            RemoveScrollBars();
            SelectDifficulty();
            SetInitialPositions();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.UpArrow)
                    {
                        MoveFirstPlayerUp();
                    }
                    else if (keyInfo.Key == ConsoleKey.DownArrow)
                    {
                        MoveFirstPlayerDown();
                    }
                }
                SecondPlayerAIMove();
                MoveBall();
                Console.Clear();
                DrawSecondPlayer();
                DrawFirstPlayer();
                DrawBall();
                PrintResult();
                Thread.Sleep(60);
            }
        }


    }
}
